package com.hou.mytomcat.myClassLoader;

import cn.hutool.core.io.FileUtil;
import sun.misc.PerfCounter;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

/**
 * web应用类加载器
 */
public class WebappClassLoader extends URLClassLoader {
    private ClassLoader parent;

    public WebappClassLoader(String docBase, ClassLoader commonClassLoader) {
        //制定它的parent类加载器是commonClassLoader
        super(new URL[] {}, commonClassLoader);
        parent = commonClassLoader;
        try {
            File webInfFolder = new File(docBase, "WEB-INF");
            File classesFolder = new File(webInfFolder, "classes");
            File libFolder = new File(webInfFolder, "lib");
            URL url;
            //加载编译好的class文件，web应用自己写的那部分类，结尾的"/"表示加载的是目录
            url = new URL("file:" + classesFolder.getAbsolutePath() + "/");
            this.addURL(url);
            //加载lib目录下引入的jar包
            List<File> jarFiles = FileUtil.loopFiles(libFolder);
            for(File file : jarFiles) {
                url = new URL("file:" + file.getAbsolutePath());
                this.addURL(url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public Class<?> loadClass(String name) throws ClassNotFoundException {
//        return loadClass(name, false);
//    }
//
//    @Override
//    protected Class<?> loadClass(String name, boolean resolve)
//            throws ClassNotFoundException
//    {
//        synchronized (getClassLoadingLock(name)) {
//            // First, check if the class has already been loaded
//            Class<?> c = findLoadedClass(name);
//            if (c == null) {
//                long t0 = System.nanoTime();
////                try {
////                    if (parent != null) {
////                        c = parent.loadClass(name, false);
////                    } else {
////                        c = findBootstrapClassOrNull(name);
////                    }
////                } catch (ClassNotFoundException e) {
////                    // ClassNotFoundException thrown if class not found
////                    // from the non-null parent class loader
////                }
//
//                if (c == null) {
//                    // If still not found, then invoke findClass in order
//                    // to find the class.
//                    long t1 = System.nanoTime();
//                    if(name.startsWith("com.hou"))
//                        c = findClass(name);
//                    else {
//                        c = this.getParent().loadClass(name);
//                    }
//
//                    // this is the defining class loader; record the stats
//                    PerfCounter.getParentDelegationTime().addTime(t1 - t0);
//                    PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
//                    PerfCounter.getFindClasses().increment();
//                }
//            }
//            if (resolve) {
//                resolveClass(c);
//            }
//            return c;
//        }
//    }

    public void stop() {
        try {
            close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
